local pojing = fk.CreateSkill {
  name = "jin_heg__pojing",
}

Fk:loadTranslationTable {
  ["jin_heg__pojing"] = "迫境",
  [":jin_heg__pojing"] = "出牌阶段限一次，你可以令一名其他角色选择一项：1.你获得其区域内一张牌;" ..
      "2.令你发起势力召唤，然后所有与你势力相同的角色和未确定势力的角色依次可以明置与你势力相同的武将牌，然后对其造成等量的伤害。",

  ["#pojing-active"] = "迫境：选择一名其他角色",
  ["#pojing-choice"] = "迫境：选择一项",
  ["pojing_getcard"] = "令 %src 获得你区域的一张牌",
  ["pojing_summmon"] = "与%src势力相同的角色可以明置武将牌，对你造成伤害",
  ["#jin_heg__pojing-ask"] = "迫境：你可以明置任意张武将牌，对 %dest 造成等量伤害",

  ["$jin_heg__pojing1"] = "奉命伐吴，得胜纳降。",
  ["$jin_heg__pojing2"] = "进军逼江，震慑吴贼。",
}

local H = require "packages.ol_hegemony.util"

pojing:addEffect("active", {
  anim_type = "offensive",
  prompt = "#pojing-active",
  can_use = function(self, player)
    return player:usedSkillTimes(pojing.name, Player.HistoryPhase) == 0
  end,
  card_filter = function() return false end,
  target_num = 1,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local choices = {}
    if not target:isAllNude() then
      table.insert(choices, "pojing_getcard:" .. player.id)
    end
    table.insert(choices, "pojing_summmon:" .. player.id)
    local choice = room:askToChoice(target, { choices = choices, skill_name = pojing.name, prompt = "#pojing-choice" })
    if choice == "pojing_getcard:" .. player.id then
      local cid = room:askToChooseCard(player, {
        target = target,
        flag = "hej",
        skill_name = pojing.name,
      })
      room:moveCardTo(cid, Card.PlayerHand, player, fk.ReasonPrey, pojing.name, nil, false, player)
    else
      local targets = table.filter(room:getAlivePlayers(),
        function(p)
          return (H.compareKingdomWith(player, p, false) and not H.allGeneralsRevealed(p))
              or (p.kingdom == "unknown" and not (H.getKingdomPlayersNum(room)[player.kingdom] >= #room.players // 2
                and not table.find(room.alive_players, function(_p) return _p.general == "jy_heg__lordsimayi" end)))
        end)
      for _, p in ipairs(targets) do
        if not p.dead then
          local main, deputy = false, false
          local kingdom = player.kingdom
          if (H.compareKingdomWith(player, p, false) or H.compareExpectedKingdomWith(p, player, false)) then
            local general = Fk.generals[p:getMark("__heg_general")]
            main = general.kingdom == kingdom or general.subkingdom == kingdom
            general = Fk.generals[p:getMark("__heg_deputy")]
            deputy = general.kingdom == kingdom or general.subkingdom == kingdom
          end
          local flag = main and "m" or ""
          if deputy then
            flag = flag .. "d"
          end
          local result = H.askToRevealGenerals(p, {
            skill_name = pojing.name,
            prompt = "#jin_heg__pojing-ask::" .. target.id,
            flag = flag,
          })
          if result ~= "Cancel" and not target.dead then
            local n = result == "md" and 2 or 1
            room:damage {
              from = p,
              to = target,
              damage = n,
              skillName = pojing.name,
            }
          end
        end
      end
    end
  end,
})

return pojing
